

package cn.shengchao.examstar.me.collect.acl.adapters.repositories;

import cn.shengchao.examstar.me.collect.acl.ports.repositories.ICollectRepository;
import cn.shengchao.examstar.me.collect.domain.CourseCollect;
import cn.shengchao.examstar.me.collect.domain.TeacherCollect;
import cn.shengchao.examstar.me.infrastructure.dao.CollectDao;
import cn.shengchao.examstar.me.infrastructure.dao.po.TCollect;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CollectRepository implements ICollectRepository {

    @Autowired
    private CollectDao collectDao;

    @Override
    public void saveCourseCollect(CourseCollect collect) {
        TCollect tCollect = new TCollect();
        tCollect.setUserId(collect.getUserId());
        tCollect.setObjectId(collect.getCourseId());
        tCollect.setTime(collect.getTime());
        tCollect.setObjectType(1);
        collectDao.insert(tCollect);
    }

    @Override
    public void cancelCollect(Long userId, Long courseId) {
        collectDao.delete(new LambdaQueryWrapper<TCollect>().eq(TCollect::getObjectId, courseId)
            .eq(TCollect::getObjectType, 1)
            .eq(TCollect::getUserId, userId));
    }

    @Override
    public List<CourseCollect> getCollect(Long userId) {
        return collectDao.selectList(new LambdaQueryWrapper<TCollect>().eq(TCollect::getUserId, userId))
            .stream()
            .map(this::of)
            .toList();
    }

    private CourseCollect of(TCollect tCollect) {
        CourseCollect collect = new CourseCollect();
        collect.setId(tCollect.getId());
        collect.setCourseId(tCollect.getObjectId());
        collect.setTime(tCollect.getTime());
        collect.setUserId(tCollect.getUserId());
        return collect;
    }

    @Override
    public void collectTeacher(TeacherCollect collect) {
        TCollect tCollect = new TCollect();
        tCollect.setUserId(collect.getUserId());
        tCollect.setObjectId(collect.getTeacherId());
        tCollect.setTime(collect.getTime());
        tCollect.setObjectType(2);
        collectDao.insert(tCollect);
    }

    @Override
    public void cancelCollectTeacher(Long userId, Long teacherId) {
        collectDao.delete(new LambdaQueryWrapper<TCollect>().eq(TCollect::getObjectId, teacherId)
            .eq(TCollect::getObjectType, 2)
            .eq(TCollect::getUserId, userId));
    }
}
